<template>
  <div class="upload-container">
    <el-button
      icon="el-icon-upload"
      size="mini"
      v-if="!dialogVisible"
      :style="{ background: color, borderColor: color, zIndex: 0 }"
      @click="dialogVisible = true"
      type="primary"
      >上传图片
    </el-button>
    <el-dialog append-to-body :visible.sync="dialogVisible">
      <el-upload
        class="editor-slide-upload"
        action="/api/mall/api-file/files-anon"
        :multiple="true"
        :file-list="fileList"
        :headers="{ Authorization: getToken() }"
        :show-file-list="true"
        list-type="picture-card"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
      >
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
export default {
  name: "editorSlideUpload",
  props: {
    color: {
      type: String,
      default: "#1890ff",
    },
  },
  data() {
    return {
      dialogVisible: false,
      // listObj: {},
      fileList: [],
      getToken: getToken,
    };
  },
  computed: {
    serverUrl() {
      return this.$store.state.app.serverUrl;
    },
  },
  methods: {
    // checkAllSuccess() {
    //   return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    // },
    handleSubmit() {
      const arr = this.fileList.map((item) => {
        let prefix = "";
        if (item.response.data.url.indexOf("//") == -1) {
          prefix = this.serverUrl;
        }
        return {
          url: prefix + item.response.data.url,
        };
      });
      console.log(arr);
      this.$emit("successCBK", arr);
      // this.listObj = {};
      this.fileList = [];
      this.dialogVisible = false;
    },
    handleSuccess(response, file, fileList) {
      if (response.code != 200) {
        fileList.pop();
      }
      this.fileList = fileList;
      // console.log(file)
      // const uid = file.uid;
      // const objKeyArr = Object.keys(this.listObj)
      // for (let i = 0, len = objKeyArr.length; i < len; i++) {
      //   if (this.listObj[objKeyArr[i]].uid === uid) {
      //     this.listObj[objKeyArr[i]].url = this.dataObj.host + '/' + this.dataObj.dir + '/' + file.name;
      //     this.listObj[objKeyArr[i]].hasSuccess = true;
      //     return
      //   }
      // }
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
      // const uid = file.uid;
      // const objKeyArr = Object.keys(this.listObj);
      // for (let i = 0, len = objKeyArr.length; i < len; i++) {
      //   if (this.listObj[objKeyArr[i]].uid === uid) {
      //     delete this.listObj[objKeyArr[i]];
      //     return
      //   }
      // }
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
}
</style>
